const Controller = require('egg').Controller
let moment = require('moment');
class ETCController extends Controller {
  constructor(ctx) {
    super(ctx)


  }

  // 创建角色
  async getEtcDoor() {
    const { ctx } = this
    let etcDoorDate = await this.ctx.model.ValidateEtc.find({})

    ctx.helper.success({ ctx, res: { data: etcDoorDate } })
  }
  async getLL() {
    const { ctx } = this
    let LL = await this.ctx.model.ValidateLL.find({})
    ctx.helper.success({ ctx, res: { data: LL } })
  }
  async test() {
    const { ctx } = this
    let tradeList = await this.ctx.model.ValidateEtcTrade.find({})
    for (let i = 0; i < tradeList.length; i++) {
      let roadLine = await this.ctx.model.RoadLine.findOne({
        门架编号: tradeList[i]['门架编号']
        ,
        收费站ID: tradeList[i]['收费站ID']
      })
      tradeList[i].roadLineId = roadLine._id
    }
    await tradeList.save()
  }
  async getVehicleByTime() {
    const { ctx } = this
    let { app } = ctx

    let { time } = ctx.request.body
    let date = moment(time, "YYYY-MM-DD HH:mm:ss");
    console.log(date)
    let between = date.unix()
    // console.log(date)
    // console.log(between)
    // let rDate = date.toDate();
    // console.log(rDate)
    // console.log(Number(rDate) / 1000)


    // //1.查询时间在5s内的订单 

    // // let tradeList = await this.ctx.model.ValidateEtcTrade.find({ 入口时间戳: { $lt: between }, 交易时间戳: { $gt: between } }, '门架编号 收费站ID')
    if (typeof (app.tradeList) == 'undefined' || app.length < app.maxLength / 2) {
      let road = []
      let tradeList = await this.ctx.model.ValidateEtcTrade.aggregate([
        {
          $match: {
            入口时间戳: { $lt: between },
            交易时间戳: { $gt: between }
          },

        }
        , {
          $project: {
            timeBt: { $subtract: [between, '$入口时间戳'] },
            车辆运行时间: 1,
            入口时间戳: 1,
            门架编号: 1,
            收费站ID: 1,
            门架名称: 1,
            收费站名称: 1,
            入口时间: 1,
            卡车型: 1,
            车辆状态标识: 1,
            车种: 1,
            卡车牌: 1,
          }
        },
      ]).sort({ "timeBt": 1 }).limit(500)

      console.log(tradeList.length)
      for (let i = 0; i < tradeList.length; i++) {
        let roadLine = await this.ctx.model.RoadLine.findOne({
          门架编号: tradeList[i]['门架编号'],
          收费站ID: tradeList[i]['收费站ID']
        })
        road[i] = JSON.parse(roadLine["轨迹json"])
        tradeList[i].length = roadLine.lineLength
        let index = Math.trunc(tradeList[i].timeBt / tradeList[i]['车辆运行时间'] * tradeList[i].length)
        tradeList[i].point = road[i][index]
        tradeList[i].currentTime = time
        tradeList[i].index = i
        // console.log("车辆已运行时间：" + tradeList[i].timeBt + " 车辆总运行时间：" + tradeList[i]['车辆运行时间'])
        // console.log("当前位置" + index + " 数组总长度：" + tradeList[i].length)
      }
      app.tradeList = tradeList
      app.road = road
      app.length = tradeList.length
      app.maxLength = tradeList.length
    } else {
      app.length = 0
      for (let i = 0; i < app.tradeList.length; i++) {

        let timeBt = between - app.tradeList[i]['入口时间戳']
        if (timeBt >= app.tradeList[i]['车辆运行时间'] || timeBt < 0) {
          continue
        }
        app.length++
        let index = Math.trunc(timeBt / app.tradeList[i]['车辆运行时间'] * app.tradeList[i].length)
        // console.log("车辆已运行时间：" + timeBt + " 车辆总运行时间：" + app.tradeList[i]['车辆运行时间'])
        // console.log("当前位置" + index + " 数组总长度：" + app.tradeList[i].length)
        app.tradeList[i].currentTime = time
        app.tradeList[i].point = app.road[i][index]
      }
    }

    // // //2.计算时间差
    // // //3.根据时间差和轨迹总时间定位轨迹点
    // // //4.解析json包并获得对应轨迹点

    // // let LL = await this.ctx.model.ValidateLL.find({})
    ctx.helper.success({ ctx, res: { data: app.tradeList } })
  }


}


module.exports = ETCController